package cn.zwx.canal.tools.listener;

import cn.zwx.canal.tools.common.bo.CovertMessageBO;
import cn.zwx.canal.tools.enums.BuisType;
import cn.zwx.canal.tools.event.DeleteAbstractCanalEvent;
import cn.zwx.canal.tools.hanlder.CovertMessageHandler;
import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @projectName:learn
 * @see:cn.zwx.canal.tools.listener
 * @author:zhangwenxue
 * @createTime:2020/12/19 14:29
 * @version:1.0
 */
@Component
public class DeleteCanalListener extends AbstractCanalListener<DeleteAbstractCanalEvent>{

    private Logger logger = LoggerFactory.getLogger(DeleteCanalListener.class);
    @Autowired
    private CovertMessageHandler covertMessageHandler;

    @Override
    protected void doChange(String database, String tableName, CanalEntry.RowData rowData) {
        logger.info("监听到删除事件:数据库名称{},表名称:{}",database,tableName);
        // 删除之前的数据
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        // 转换发送消息类
        CovertMessageBO messageBO = new CovertMessageBO();
        messageBO.setDatabase(database);
        messageBO.setBusiType(BuisType.DELETE.getCode());
        messageBO.setTableName(tableName);
        messageBO.setBeforeColumnsList(beforeColumnsList);
        Map<String, Object> messageMap = covertMessageHandler.covertMessageDTO(messageBO);
        // 发送MQ
        logger.info(String.format("{%s},发送mq请求参数:",BuisType.DELETE.getMsg())+ JSON.toJSONString(messageMap));
        super.sendMessage(messageMap);

    }
}
